package com.xjm.service.serviceImpl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.lang.UUID;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.xjm.constant.AppConst;
import com.xjm.entity.dto.AdminDto;
import com.xjm.entity.model.AdminModel;
import com.xjm.entity.vo.AdminVo;
import com.xjm.entity.vo.LoginAdminVo;
import com.xjm.excption.AppException;
import com.xjm.mapper.AdminMapper;
import com.xjm.service.AdminService;
import com.xjm.service.MenuService;
import com.xjm.utils.RedisUtil;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;

@Service
@RequiredArgsConstructor
public class AdminServiceImpl extends ServiceImpl<AdminMapper, AdminModel> implements AdminService {

    private final RedisUtil redisUtil;

    private final AdminMapper adminMapper;

    private final MenuService menuService;

    @Override
    public LoginAdminVo login(String account, String password) {
        String key = AppConst.ADMIN_PREFIX + account;

        //判断是否存在登陆记录，有效期一小时，不存在则创建
        if (!redisUtil.hasKey(key)) {
            redisUtil.set(key, 0, 60 * 60);
        }

        //判断密码错误是否超过5次
        if ((Integer) redisUtil.get(key) >= 5) {
            throw new AppException("账号已被临时封禁，请在" + redisUtil.getExpire(key) + "秒后重试");
        }

        LambdaQueryWrapper<AdminModel> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(AdminModel::getAccount, account);

        AdminModel userByAccount = adminMapper.selectOne(wrapper);
        //判断用户是否存在
        if (userByAccount == null) {
            throw new AppException("用户不存在");
        }
        //判断用户状态是否正常
        if (!userByAccount.getStatus().equals(1)) {
            throw new AppException("账号已被封禁");
        }
        //判断密码是否一致
        if (!userByAccount.getPassword().equals(password)) {
            if (redisUtil.increment(key, 1) >= 5) {
                redisUtil.setExpire(key, 30);
            }
            throw new AppException("密码错误");
        }

        LoginAdminVo adminVo = new LoginAdminVo();
        BeanUtil.copyProperties(userByAccount, adminVo);
        String token = UUID.fastUUID().toString();
        redisUtil.set(AppConst.TOKEN_PREFIX + token, userByAccount, 60 * 60 * 24);
        adminVo.setToken(token);
        adminVo.setMenus(menuService.getAdminList(userByAccount.getId()));
        return adminVo;
    }

    @Override
    public Boolean addAdmin(AdminDto adminDto) {
        LambdaQueryWrapper<AdminModel> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(AdminModel::getAccount, adminDto.getAccount());

        AdminModel userByAccount = adminMapper.selectOne(wrapper);
        if (userByAccount != null) {
            throw new AppException("账号已存在");
        }
        AdminModel adminModel = BeanUtil.copyProperties(adminDto, AdminModel.class);
        adminModel.setStatus(1);
        return adminMapper.insert(adminModel) != 0;
    }

    @Override
    public AdminVo getAdminById(Integer id) {
        AdminModel adminModel = adminMapper.selectById(id);
        if (adminModel == null) {
            throw new AppException("用户不存在");
        }
        return BeanUtil.copyProperties(adminModel, AdminVo.class);
    }

    @Override
    public boolean updateAdmin(AdminDto adminDto) {
        AdminModel adminModel = BeanUtil.copyProperties(adminDto, AdminModel.class);
        return adminMapper.updateById(adminModel) != 0;
    }

    @Override
    public boolean deleteAdmin(Integer id) {
        return adminMapper.deleteById(id) != 0;
    }
}
